#!/usr/anaconda/env python
# -*- coding: utf-8 -*-
# author: uestcwdh
# @Time: 2020/3/9 15:21
import networkx as nx
import matplotlib.pyplot as plt


def draw_graph(degree_lst):
    g = nx.Graph()
    nodes = [i for i in range(len(degree_lst))]
    g.add_nodes_from(nodes)
    for i in range(len(degree_lst)):
        count = degree_lst[i]
        for j in range(i + 1, len(degree_lst)):
            if degree_lst[j] != 0:
                count -= 1
                g.add_edge(i, j)
                degree_lst[j] -= 1
            if count == 0:
                break

    position = nx.circular_layout(g)
    nx.draw_networkx_nodes(g, position, nodelist=nodes, node_color="r")
    nx.draw_networkx_edges(g, position)
    nx.draw_networkx_labels(g, position)
    plt.show()


def equal_transfer(lst):
    first_degree = lst[0]
    part1 = [(item - 1) for item in lst[1: first_degree + 1]]
    part2 = [item for item in lst[first_degree + 1: len(lst) + 1]]
    res = part1 + part2
    return res


def is_graph_seq(lst):
    new_seq = sorted(lst, reverse=True)
    print("等价判定序列转换过程如下：")
    while min(new_seq) >= 0:
        non_zero_lst = [item for item in new_seq
                        if item > 0]
        if min(new_seq) < 0 or sum(new_seq) % 2 == 1 or max(new_seq) >= len(non_zero_lst):
            return False
        new_seq = sorted(equal_transfer(new_seq), reverse=True)
        print(new_seq)
        if sum(new_seq) == 0:
            return True


if __name__ == '__main__':
    print("请输入需要判定的序列（用逗号隔开）:")
    s = input()
    in_s = s.split(',')
    in_s = [int(item) for item in in_s]
    if is_graph_seq(in_s):
        print("结论:\n" + s + "是图序列")
        print("该图序列对应的一个简单图为:")
        draw_graph(sorted(in_s, reverse=True))
    else:
        print("结论:\n" + s + "不是图序列")